
/**
 * @file option.c
 * @author      JinChengLong (https://gitee.com/Datejin/)
 * @brief       操作界面文件
 * @version     0.1
 * @date        2023
 * 
 * @copyright   Copyright © 2025 by JinChengLong 
 * 
 * All rights reserved.
 * Unauthorized copying, reproduction, or redistribution of this file via any medium is strictly prohibited.  
 * Proprietary and confidential.
 *   
 */
#include "option.h"


uint8_t C[19200],C2[19200],stand0[1280],stand1[1280],B[1680],D[1680],E[1680],B1[1680],D1[1680],E1[1680],F[1800],bizhi[1],bi[1],DHT[4],DHT0[4],daiji_time[1]={20};//
uint32_t t,si,ii,jj,ALarm=0;

/*真*初始化*/
/**
 * @brief 初始化所有设备
 * 
 */
void INIT(void )
{

	
	
	ledkey_init();//按键灯初始化
	
	encoder_init();//编码器初始化
	
	LCD_Init();//LCD屏初始化
	
	W25Q64_init();//W25Q64//FLASH存储初始化
	
	RTCa_init();//RTC时钟初始化（已写入一遍可去除）
	
	usart_init();//串口初始化

	stand_init();//待机初始化
	
	W25_BACK();//W25Q64读取
	
	LCD_Fill (0,0,128,160,WHITE );
	LCD_ShowString(30,80,"WHITE...",BLUE ,WHITE ,24,0);
	Delay_ms (200);
	
	
	
}

/*背景*/
/**
 * @brief 渲染背景
 * 
 */
void background(void )
{

	encoder_over();
	W25Q64_readdata(4096*5*40,C,19200);
	W25Q64_readdata(4096*5*40+19200,stand0,1280);
	W25Q64_readdata(4096*5*40+20480,C2,19200);
   W25Q64_readdata(4096*5*40+20480+19200,stand1,1280);
	
	LCD_ShowPicture(0,0,128,80,C);
	LCD_ShowPicture(0,75,128,5,stand0);
	LCD_ShowPicture(0,80,128,80,C2);
	LCD_ShowPicture(0,155,128,5,stand1);
}







/*主选项*/
/**
 * @brief 主界面选项
 * 
 */
void option_primary(void )
{
	stand_by();
	BeiJing_time();
	zhujiemian_mianban();
	if(key1() ==1)
	{
		  if(conde==4 )
		  {
				set_up();
		  }
		   else if(conde==8 )
		  {
			option_secondary();
		  }
	}
}
/*菜单*/
/**
 * @brief 菜单选项
 * 
 */
void option_secondary(void)
{
  option_secondmenu();
  while(1)
  {
	 stand_by();
	  fujiemian_mianban_switch();
	if(key1()==1)			
	{
		Delay_ms (200);
		
	    if(conde == 4)
		{
			option_car();			
		}
		else if(conde == 8)
		{
			 option_DHT();
		}
	
		else if(conde ==12)	
		{
			
			option_album();
		}
		else if(conde ==44)
		{
			option_mainmenu();
			break ;
		}	
	 }
	
	}
}
	

/*设置*/
/**
 * @brief Set the up object
 * 
 */
void set_up(void )
{
	
	background();
	while(1)
	{
		SETjimian();
		if(conde != condem )
		{
		 ass=RTC_GetCounter()+daiji_time[0];
		}
		
		if(key1()==1)
		{
			if(conde ==4)
			{
				set_password();
			}
			else if(conde ==8)
			{
				set_standtime();
				
			}
		}
		else if(key0()==1)
		{
			option_mainmenu();
			break;
		}
		condem=conde;
	}
}



/*设置密码*/
/**
 * @brief Set the password object
 * 
 */
void set_password(void)
{
	background ();
	mima[0]=0;mima[1]=0;mima[2]=0;mima[3]=0;
	while (1)
	{
		
		LCD_ShowChinese(15,130,"请输入原密码",BRED,BLACK,16,0);
		mimajiemian(0);

		if(conde !=20)
		{
			mima_key(conde);
		}
		else if(conde==20  & mima_P() ==1 & key1()==1)
		{
			while (1)
			{
				LCD_ShowChinese(15,130,"请输入新密码",GREEN,BLACK,16,0);
				
				mimajiemian(0);
				if(conde !=20)
				{
					mima_key(conde);
				}
				else if(conde==20&key1()==1)//
				{
					for(si=0;si<4;si++)
					{
						ma[si]=mima[si];
						
					}
					W25Q64_clearerase(4096*131);
					neicun_write(4096*131,4,ma);
					W25Q64_readdata(4096*131,ma,4);
					LCD_ShowString(60,80,"OK",BLACK ,WHITE ,16,0);
					Delay_ms (600);
					break;
				}
				else if(key0()==1)
				{
					
					break ;
				}
			}
			
			Delay_ms (200);
		}
		if(key0()==1)
		{
			background ();
			break;
			
		}
	
	}
	
}
/*设置待机时间*/
/**
 * @brief Set the standtime object
 * 
 */
void set_standtime(void)
{
	background();
	
	while(1)
	{
		standjimian();
		LCD_ShowIntNum(51,80,daiji_time[0],3,BLUE  ,LIGHTBLUE ,16);
		
		if(key1()==1)
		{
			if(conde ==4)
			{
				daiji_time[0]=daiji_time[0] +10;
			}
			else if (conde ==8)
			{
				W25Q64_clearerase(4096*136);
				neicun_write(4096*136,1,daiji_time);
				LCD_ShowString(60,100,"OK",BLACK ,WHITE ,16,1);
				Delay_ms (600);
				
			}
		}
		else if(key0()==1 )
		{
			if(daiji_time[0]>=10)
			{
				daiji_time[0]=daiji_time[0] -10;
			}
			if(conde ==12)
			{
				background();
				break;
			}
			
		}
	
	

	}

}	
		
		

/*相册->选择头像*/
/**
 * @brief 相册界面选择头像
 * 
 */
void option_album(void)
{

	background ();
	t=0;
	encoder_over();
	while (1)
	{
		
		if(encoder_get()>=0)
		{
			t=S(encoder_get()%40,4);
		}
	
		else if(encoder_get()<0)
		{
			
			t=10-S((-encoder_get())%40,4);	
		}
		if(t==10)
		{
			t=0;
		}
		if(t != condem )
		{
			 ass=RTC_GetCounter()+10;
		}
		picture_du(t,C);
		LCD_ShowPicture(15,15,100,96,C);
		LCD_ShowIntNum(58,130,t+1,2,BLUE ,WHITE ,16);
		
		if(key1()==1)			
		{
			bi[0]=t;
			W25Q64_clearerase(4096*130);
			neicun_write(4096*130,1,bi);
			W25Q64_readdata(4096*130,bizhi ,1);
			LCD_ShowChinese(52,63,"已选择",RED,WHITE,12,0);
		}
		if(key0()==1)
		{
			option_secondmenu();
			break ;
		}
		condem=t;
	}
}





/*小车->蓝牙控制*/
/**
 * @brief 小车界面控制小车
 * 
 */
void option_car(void)
{
	
	background ();
	encoder_over();
	Delay_ms (200);
	LCD_ShowChinese(36,144,"驾驶时间",YELLOW,BLACK ,16,0);
	Delay_ms (200);
	LCD_ShowChinese(10,15,"前",CYAN,BLACK ,16,0);
	LCD_ShowChinese(102,15,"后",CYAN,BLACK ,16,0);
	LCD_ShowChinese(10,45,"左",LIGHTGREEN,BLACK ,16,0);
	LCD_ShowChinese(102,45,"右",LIGHTGREEN,BLACK ,16,0);
	LCD_ShowChinese(10,80,"顺",LIGHTBLUE,BLACK ,16,0);
	LCD_ShowChinese(102,80,"逆",LIGHTBLUE,BLACK ,16,0);
	LCD_ShowChinese(10,115,"停",GRAY,BLACK ,16,0);
	LCD_ShowChinese(102,115,"退",GRAY,BLACK ,16,0);
	usart_sendstring("@xiaoche .");
	while(1)
	{	
		
		 carjiemian_mianban();
		if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)==0)
		{
		
			switch (conde )
			{
				case 4:usart_sendstring("@da .");break;
				case 8:usart_sendstring("@z .");break;
				case 12:usart_sendstring("@C .");break;
				case 16:usart_sendstring("@laba .");break;
			}
		}
		else  if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0)
		{
			 switch (conde )
			 {
				 case 4:usart_sendstring("@H .");break ;
				 case 8:usart_sendstring("@y .");break ;
				 case 12:usart_sendstring("@Cr .");break ;
			 }
			 if(conde ==16)
			 {
				option_secondmenu();break ;
			 }
		 }
		 else 
		 {
			  usart_sendstring("@i .");
		  }
	}
	
}


/*温湿度->显示->保存数据*/
/**
 * @brief 温湿度界面显示数据
 * 
 */
void option_DHT(void)
{
	background ();
	Delay_ms (200);
	usart_sendstring("@TandD .");
	LCD_Fill (10,70,116,98,BLACK );
	LCD_Fill (10,20,116,48,BLACK );

	while(1)
	{
		DHjiemian();
		
		if(usart_getRxflag()==1)
		{
			
	
			LCD_ShowChinese(10,35,"湿度",CYAN,LIGHTBLUE,12,1);
			LCD_ShowString(34,35,":",CYAN,LIGHTBLUE,12,1);		
			LCD_ShowIntNum(40,35,usart_123ascll(Serial_RxPacket,0,2),2,BLUE ,LIGHTBLUE ,12);
			LCD_ShowString(52,35,".",DARKBLUE,LIGHTBLUE,12,0);	
			LCD_ShowIntNum(55,35,usart_123ascll(Serial_RxPacket,2,1),1,BLUE ,LIGHTBLUE ,12);
		
			LCD_ShowChinese(64,35,"温度",GRED,LIGHTBLUE,12,1); 
			LCD_ShowString(88,35,":",GRED,WHITE,12,1);	
			LCD_ShowIntNum(94,35,usart_123ascll(Serial_RxPacket,3,2),2,BLUE ,LIGHTBLUE ,12);
			LCD_ShowString(106,35,".",DARKBLUE,LIGHTBLUE,12,0);
			LCD_ShowIntNum(109,35,usart_123ascll(Serial_RxPacket,5,1),1,BLUE ,LIGHTBLUE ,12);
		}
		if(Read_DHT11()==1)
		{	
			Delay_ms (100);	
				
			LCD_ShowChinese(10,20,"湿度",CYAN,LIGHTBLUE,12,1);
			LCD_ShowString(34,20,":",CYAN,LIGHTBLUE,12,1);
			LCD_ShowIntNum(40,20,H11,2,BLUE ,LIGHTBLUE ,12);	
			LCD_ShowString(52,20,".",DARKBLUE,LIGHTBLUE,12,0);
			LCD_ShowIntNum(55,20,h11,1,BLUE ,LIGHTBLUE ,12);	
			
			LCD_ShowChinese(64,20,"温度",GRED,LIGHTBLUE,12,1);
			LCD_ShowString(88,20,":",GRED,LIGHTBLUE,12,1);	
			LCD_ShowIntNum(94,20,T11,2,BLUE ,LIGHTBLUE ,12);							
			LCD_ShowString(106,20,".",DARKBLUE,LIGHTBLUE,12,0);
			LCD_ShowIntNum(109,20,t11,1,BLUE ,LIGHTBLUE ,12);	
			
			
		if(key1()==1)
		{
			if(conde ==4)
			{
				DHT[0]=usart_123ascll(Serial_RxPacket,0,2);DHT[1]=usart_123ascll(Serial_RxPacket,2,1);
				DHT[2]=usart_123ascll(Serial_RxPacket,3,2);DHT[3]=usart_123ascll(Serial_RxPacket,5,1);
				DHT0[0]=H11;DHT0[1]=h11;DHT0[2]=T11;DHT0[3]=t11 ;
				W25Q64_clearerase(4096*(132+jj)) ;
				neicun_write(4096*(132+jj),4,DHT);//
				neicun_write(4096*(132+jj)+256,4,DHT0);//
				
				
			}
			else if(conde ==8)
			{
				DHT[0]=usart_123ascll(Serial_RxPacket,0,2);DHT[1]=usart_123ascll(Serial_RxPacket,2,1);
				DHT[2]=usart_123ascll(Serial_RxPacket,3,2);DHT[3]=usart_123ascll(Serial_RxPacket,5,1);
				DHT0[0]=H11;DHT0[1]=h11;DHT0[2]=T11;DHT0[3]=t11 ;

				W25Q64_readdata(4096*(132+jj),DHT,4);
				W25Q64_readdata(4096*(132+jj)+256,DHT0,4);
				
				//LCD_ShowChinese (10,50,"内存",BLACK,WHITE ,16,0);
				LCD_ShowChinese(10,85,"湿度",BROWN,LIGHTBLUE,12,1);
				LCD_ShowString(34,85,":",BROWN ,LIGHTBLUE,12,1);
				LCD_ShowIntNum(40,85,DHT[0],2,BLUE ,LIGHTBLUE ,12);	
				LCD_ShowString(52,85,".",DARKBLUE,LIGHTBLUE,12,0);
				LCD_ShowIntNum(55,85,DHT[1],1,BLUE ,LIGHTBLUE ,12);	
				
				LCD_ShowChinese(64,85,"温度",YELLOW,LIGHTBLUE,12,1);
				LCD_ShowString(88,85,":",YELLOW,LIGHTBLUE,12,1);	
				LCD_ShowIntNum(94,85,DHT[2],2,BLUE ,LIGHTBLUE ,12);							
				LCD_ShowString(106,85,".",DARKBLUE,LIGHTBLUE,12,0);
				LCD_ShowIntNum(109,85,DHT[3],1,BLUE ,LIGHTBLUE ,12);
				
				
				LCD_ShowChinese(10,70,"湿度",BROWN,WHITE,12,1);
				LCD_ShowString(34,70,":",BROWN ,WHITE,12,1);
				LCD_ShowIntNum(40,70,DHT0[0],2,BLUE ,LIGHTBLUE ,12);	
				LCD_ShowString(52,70,".",DARKBLUE,LIGHTBLUE,12,0);
				LCD_ShowIntNum(55,70,DHT0[1],1,BLUE ,LIGHTBLUE ,12);	
				
				LCD_ShowChinese(64,70,"温度",YELLOW,WHITE,12,1);
				LCD_ShowString(88,70,":",YELLOW,LIGHTBLUE,12,1);	
				LCD_ShowIntNum(94,70,DHT0[2],2,BLUE ,LIGHTBLUE ,12);							
				LCD_ShowString(106,70,".",DARKBLUE,LIGHTBLUE,12,0);
				LCD_ShowIntNum(109,70,DHT0[3],1,BLUE ,LIGHTBLUE ,12);
			}
			else if(conde ==12)
			{
				jj=jj+1;
				
				if(jj==4)
				{
					jj=0;
				}
				LCD_ShowIntNum(61,100,jj,1,BLACK ,WHITE ,16);
			}
			
		}
		else if(key0()==1)
		{
			option_secondmenu();
			break ;
		}
	  }
	
	}
}

/*主界面*/
/**
 * @brief 主菜单界面
 * 
 */
void option_mainmenu (void)
{
	
	//LCD_Fill(0,0,128,160,WHITE );
	background();
	W25Q64_readdata(4096*100,B,1680);
	W25Q64_readdata(4096*101,D,1680);
	W25Q64_readdata(4096*102,E,1680);
	LCD_ShowPicture(15,118,30,28,D);
	LCD_ShowPicture(50,118,30,28,B);
	LCD_ShowPicture(85,118,30,28,E);
	picture_du(bizhi[0],C);
	LCD_ShowPicture(15,15,100,96,C);
	ALarm=0;
	
}

/*菜单*/
/**
 * @brief 返回菜单
 * 
 */
void option_secondmenu(void)
{		
	background();
	usart_sendstring("@R .");
}

/*待机*/
/**
 * @brief 待机设置
 * 
 */
void stand_by(void)
{
	
	ALarm= ass;
	RTC_SetAlarm(ALarm);
	if(ALarm - RTC_GetCounter()==0)
	{
		PWR_EnterSTANDBYMode();
	//先清除WAKEUP标志位//选择STANDBY模式PDDS置一进入待机模式//SLEEPDEEP置一进入深度睡眠//调用WFI进入待机模式	
	}
	
}
/*待机初始化*/
/**
 * @brief 待机初始化
 * 
 */
void stand_init(void )
{

	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);//使能PWR时钟
	PWR_WakeUpPinCmd(ENABLE);//唤醒深度睡眠开启
	ALarm=RTC_GetCounter()+daiji_time[0];
	ass=ALarm;
	
	W25Q64_readdata(4096*136,daiji_time,1);
}		
		

		
		


